<?php 

class otherController extends appController
{
	public function random()
	{
		if($_POST)
		{
			$res = array('error' => 1, 'content' => '');
			$status = yf_empty(intval($_POST['status']), 0, true);
			if($status == 0)
			{
				$clear = Model('other')->clear_db();
				if(!$clear)
				{
					$res['content'] = "系统错误";
					$this->showMsg($res);
				}
			}
			
			$str1 = "0123456789";
			$str2 = "abcdefghijklmnopqrstuvwxyz";
			$str3 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
			
			$type = yf_empty(intval($_POST['type']), 0, true);
			if($type == 0)
			{
				$str = $str1.$str2.$str3;
			}
			elseif($type == 1)
			{
				$str = $str1;
			}
			elseif($type == 2)
			{
				$str = $str2;
			}
			elseif($type == 3)
			{
				$str = $str3;
			}
			elseif($type == 4)
			{
				$str = $str1.$str2;
			}
			elseif($type == 5)
			{
				$str = $str1.str3;
			}
			elseif($type == 6)
			{
				$str = $str2.$str3;
			}
			
			$num = yf_empty(intval($_POST['num']), "请输入长度");
			$count = yf_empty(intval($_POST['count']), "请输入数量");
			$patterm = yf_empty($_POST['patterm'], "", true);
			$type_num = yf_empty($_POST['type_num'], '', true);
			$num_arr = array();
			if(!empty($type_num))
			{
				$num_arr = explode(",", $type_num);
				if(count($num_arr) != 3)
				{
					$res['content'] = "输入的字符数量格式不正确";
					$this->showMsg($res);
				}
				else
				{
					if($num != ($num_arr[0] + $num_arr[1] + $num_arr[2]))
					{
						$res['content'] = "输入的字符数量加起来的和不正确";
						$this->showMsg($res);
					}
				}
			}
			
			$i = 0;
			if(empty($patterm))
			{
				if(empty($num_arr))
				{
					while($i < $count)
					{
						$arr = "";
						for($j = 0; $j < $num; $j++)
						{
							$arr .= $str[rand(0, hy_strlen($str) - 1)];
						}
						$is_exist = Model('other')->check_is_exist($arr);
						if($is_exist == 0)
						{
							$id = Model('other')->insert_random($arr);
							if($id)
							{
								$i++;
							}
							else 
							{
								$res['content'] = "系统错误";
								$this->showMsg($res);
							}
						}
					}
				}
				else 
				{
					while($i < $count)
					{
						$a = 0;
						$b = 0;
						$c = 0;
						$arr = "";
						for($j = 0; $j < $num; $j++)
						{
							$status = 1;
							while($status)
							{
								$temp = $str[rand(0, hy_strlen($str) - 1)];
								if(strpos($str1, $temp))
								{
									if($a < $num_arr[0])
									{
										$arr .= $temp;
										$status = 0;
										$a++;
									}
								}
								if(strpos($str2, $temp))
								{
									if($b < $num_arr[1])
									{
										$arr .= $temp;
										$status = 0;
										$b++;
									}
								}
								if(strpos($str3, $temp))
								{
									if($c < $num_arr[2])
									{
										$arr .= $temp;
										$status = 0;
										$c++;
									}
								}
							}
						}
						$is_exist = Model('other')->check_is_exist($arr);
						if($is_exist == 0)
						{
							$id = Model('other')->insert_random($arr);
							if($id)
							{
								$i++;
							}
							else
							{
								$res['content'] = "系统错误";
								$this->showMsg($res);
							}
						}
					}
				}
			}
			else 
			{
				$patterm_type = explode("-", $patterm);
				if(count($patterm_type) == 1)
				{
					$patterm_type = explode(",", $patterm);
					if(count($patterm_type) == 1)
					{
						$res['content'] = "输入的字符规则不正确,请使用“-”或者“,”";
						$this->showMsg($res);
					}
					else
					{
						$patterm_code = ",";
					}
				}
				else 
				{
					$patterm_code = "-";
				}
				
				$temp_num = 0;
				foreach($patterm_type as $k => $v)
				{
					$temp_num += $v;
				}
				
				if($temp_num != $num)
				{
					$res['content'] = "输入的字符规则数量和不正确";
					$this->showMsg($res);
				}
				
				if(empty($num_arr))
				{
					while($i < $count)
					{
						$key = array();
						foreach($patterm_type as $kk => $vv)
						{
							$arr_temp = "";
							for($j = 0; $j < $vv; $j++)
							{
								$arr_temp .= $str[rand(0, hy_strlen($str) - 1)];
							}
							$key[] = $arr_temp;
						}
						$arr = implode($patterm_code, $key);
						$is_exist = Model('other')->check_is_exist($arr);
						if($is_exist == 0)
						{
							$id = Model('other')->insert_random($arr);
							if($id)
							{
								$i++;
							}
							else
							{
								$res['content'] = "系统错误";
								$this->showMsg($res);
							}
						}
					}
				}
				else
				{
					while($i < $count)
					{
						$a = 0;
						$b = 0;
						$c = 0;
						$key = array();
						foreach($patterm_type as $kk => $vv)
						{
							$arr_temp = "";
							for($j = 0; $j < $vv; $j++)
							{
								$status = 1;
								while($status)
								{
									$temp = $str[rand(0, hy_strlen($str) - 1)];
									if(strpos($str1, $temp))
									{
										if($a < $num_arr[0])
										{
											$arr_temp .= $temp;
											$status = 0;
											$a++;
										}
									}
									if(strpos($str2, $temp))
									{
										if($b < $num_arr[1])
										{
											$arr_temp .= $temp;
											$status = 0;
											$b++;
										}
									}
									if(strpos($str3, $temp))
									{
										if($c < $num_arr[2])
										{
											$arr_temp .= $temp;
											$status = 0;
											$c++;
										}
									}
								}
							}
							$key[] = $arr_temp;
						}
						$arr = implode($patterm_code, $key);
						$is_exist = Model('other')->check_is_exist($arr);
						if($is_exist == 0)
						{
							$id = Model('other')->insert_random($arr);
							if($id)
							{
								$i++;
							}
							else
							{
								$res['content'] = "系统错误";
								$this->showMsg($res);
							}
						}
					}
				}
			}
			$res = array('error' => 0, 'content' => "生成成功");
			$this->showMsg($res);
		}
		$this->display("random.html");
	}
	
	public function dao_random()
	{
		header("Content-type:text/html;charset=utf-8");
		$count = Model('other')->check_num();
		if($count > 0)
		{
			$info = Model('other')->get_code();
			$table = array('序号', '随机码');
			yf_daochu($table, $info, "生成随机码");
			$msg = "导出成功";
		}
		else
		{
			$msg = "你没有生成数据,不能导出";
		}
		echo $msg;
	}
	
	public function qrcode()
	{
		if($_POST)
		{
			$url = yf_empty($_POST['url'], "请输入要生成二维码的链接");
			$size = yf_empty(intval($_POST['size']), 10, 'size');
			$name = yf_empty($_POST['name'], '', true);
			$status = yf_empty(intval($_POST['status']), 0, true);
			if($status == 0)
			{
				$path = yf_qrcode($url, $size, true, $name);
				yf_down($path);
			}
			else
			{
				$path = yf_qrcode($url, $size);
			//	$this->assign("code", $path);
			}
		}
		
		$this->display("qrcode.html");
	}
	
}

?>